

const state = {
    //会话列表
    conversationList:[
        /* {
            //userID,   //对方的ID
            //conversationID,
            //lastMessage,
            //type,
            //historyMsgList
        } */
    ],

    count: 0,
    receiveMsg : {
        flow:'',
        content:''
    },
    //当前用户信息
    curUserInfo:{
        nickname:'',
        userId:''
    }

}


const getters={
    lastChatTime:(state,getters)=>{
        return state.conversationList[0].lastMessage.lastTime
    }
}


const actions ={
    //请求当前用户信息
    
}

const mutations = {


    //更新最新的一条消息
    UPDATE_NEW_MSG(state,data){
        console.log('收到最新的文本消息，我显示到页面上了！',data);
        state.receiveMsg.flow = "in";
        state.receiveMsg.content = data;
        state.count++;
    },

    //更新会话列表
    UPDATE_CVST_LIST(state,data){
        console.log('更新会话列表了');
        //data是一个会话对象实例数组
        data.forEach(value => {
            let existFlag = false;
            state.conversationList.forEach((stateList,index)=>{
                //若已有当前会话，直接改变已存在的会话信息，而不是新增会话
                if (stateList.conversationID == value.conversationID){
                    state.conversationList[index].lastMessage = value.lastMessage;
                    state.conversationList[index].type = value.type;
                    existFlag = true;
                }
            })

            //若不存在当前会话，则push
            if (!existFlag){
                let sessionMsg = {
                    conversationID:value.conversationID, //会话ID（C2C105）
                    lastMessage:value.lastMessage,    //上一条消息包括lastMessage 1.lastTime 2.messageForShow 3.payload 
                    type:value.type
                }   
                //添加到state的数组中
                state.conversationList.push(sessionMsg);
            }

            /* 给数组元素按时间顺序排序 */
            state.conversationList.sort((a,b)=>{
                return b.lastMessage.lastTime-a.lastMessage.lastTime;
            })
        });
    },

}

export default {
    namespaced:true,
    state,
    getters,
    actions,
    mutations,
}